

$(document).ready(function(){
    getSjyml();

})

//把空的节点默认为null
function trim_nulls(data) {
    var y;
    for (var x in data) {
        y = data[x];
        y   =   y.replace(/^\s+|\s+$/g,"");
        if (y ==="") {
            data[x]=null;
        }
    }
    return data;
}

function getSjyml(){
    var sjjdm = Common.GetRequest().sjjdm;
    var sjydm = Common.GetRequest().sjydm;
    var limit=1;
    var offset=0;
    $.ajax({
        type: "POST",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        url: 'dataSet/querySjyml',
        dataType: 'json',
        data:{limit:limit, offset:offset,sjjdm:sjjdm,sjydm:sjydm},
        success: function (jsonData) {
                console.log(jsonData.rows[0]);
                //表单自动赋值
                //$('#sjyml').populateForm(lowerJSONKey(jsonData.rows[0]))
                var rows=jsonData.rows[0];
                $('#sjybsf').val(rows.SJYBSF);
                $('#sjymc').val(rows.SJYMC);
                $('#sjylx').val(rows.SJYLX);
                $('#sjkzdm').val(rows.SJKZDM);
                //选中
                var  sfzj=rows.SFZJ;
                 var  sfwk=rows.SFWK;
                if(sjjdm && sfzj!=null){
                    var numbers = $("#sfzj").find("option"); //获取select下拉框的所有值
                    for (var j = 0; j < numbers.length; j++) {
                        if ($(numbers[j]).val() == sfzj) {
                            $(numbers[j]).prop("selected", "selected");
                        };
                    }
                }
                if(sjjdm && sfwk!=null){
                    var numbers = $("#sfwk").find("option"); //获取select下拉框的所有值
                    for (var j = 0; j < numbers.length; j++) {
                        if ($(numbers[j]).val() == sfwk) {
                            $(numbers[j]).prop("selected", "selected");
                        };
                    }
                }
        }
    });
}


function cancel() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

function saveSjyml(){
    var sjjdm = Common.GetRequest().sjjdm;
    var sjydm = Common.GetRequest().sjydm;

    var data = $('form').serializeJson();
    data.sjydm=sjydm;
    data.sjjdm=sjjdm;
    //把""改成null，让后台非空字段报错
    data=trim_nulls(data);
    var formJSON = JSON.stringify(data);

    $.ajax({
        type: "POST",
        contentType: 'application/json',
        dataType: "json",
        url:  'dataSet/updateSjyml',
        data: formJSON,
        success: function (jsonData) {
            if (jsonData.code == 200) {
                    LayerWn.showSucAlert('保存成功！');
                    for (var k = 0; k < parent.frames.length; k++) {
                        if (parent.frames[k].frameElement.src.indexOf("psms-sjjgl-sjyml.html") != -1)
                            parent.frames[k].initSjyTable();
                    }

                    setTimeout(function () {
                        cancel();
                    }, 1000);
            } else {
                LayerWn.showFailAlert('服务器异常,请联系管理员');
            }
        }
    });
}